<?php
/**
 * 门店职位
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:28
 */
namespace App;


use Model\StorePositionMod;
use Model\StoreStaffMod;

class StorePosition extends Action
{
    private $StorePosition = null;   //门店职位
    private $StoreStaff = null;  //门店员工

    public function __construct()
    {
        parent::__construct();
        $this->StorePosition = new StorePositionMod();
    }


    //S-----前端门店管理------

    /**
     * 新增职位
     * @param data.name 职位名称  json_encode(data.authority) 职位权限  data.store_id 门店id
     */
    public function add()
    {
        $this->_gpc['data']['authority'] = json_encode($this->_gpc['data']['authority']);
        if($this->StorePosition->insert($this->_gpc['data'])){
            $this->_result = array('status'=>'success','data'=>pdo_insertid());
        }
        $this->result($this->_result);
    }

    /**
     * 修改职位
     * @param data.name 职位名称  json_encode(data.authority) 职位权限
     */
    public function edit()
    {
        $this->_gpc['data']['authority'] = json_encode($this->_gpc['data']['authority']);
        if($this->StorePosition->update($this->_gpc['data'],array('id'=>$this->_gpc['position_id']))){
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
    }

    /**
     * 获取门店所有职位
     * @param store_id 门店id
     */
    public function li()
    {
        $_where = array('store_id'=>$this->_gpc['store_id']);
        $_list = $this->StorePosition->findAll($_where,array('order'=>array('id','desc')),array('id','name','authority'));
        if(count($_list) > 0){
            foreach($_list as $_k=>$_v){
                $_list[$_k]['authority'] = json_decode($_v['authority'],true);
            }
            $this->_result = array('status'=>'success','data'=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 删除指定职位
     * @param position_id  职位id
     */
    public function delete()
    {
        if($this->StorePosition->delete(array('id'=>$this->_gpc['position_id']))){
            $this->StoreStaff = new StoreStaffMod();
            if($this->StoreStaff->findOne(array('position_id'=>$this->_gpc['position_id']),array('id'))){
                $this->StoreStaff->delete(array('position_id'=>$this->_gpc['position_id']));
            }
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
    }
    //E-----前端门店管理------


}